package com.bkhc.partner.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkhc.common.annotation.ApiOperation;
import com.bkhc.common.constant.RdisKeyContant;
import com.bkhc.common.entity.ApiInfo;
import com.bkhc.common.permissions.bean.UserPermission;
import com.bkhc.common.service.ApiInfoService;
import com.bkhc.common.service.UserService;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import com.bkhc.partner.entity.Partner;
import com.bkhc.partner.service.PartnerSerivce;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@Component
public class PartnerDataFilter implements Filter {

    @Override
    public void doFilter(ServletRequest var1, ServletResponse var2, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) var1;
        HttpServletResponse response = (HttpServletResponse) var2;
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class);
        ApiInfoService apiInfoService = context.getBean(ApiInfoService.class);
        HandlerExecutionChain hanler = null;
        try {
            hanler = mapping.getHandler(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (hanler == null) {
            //取不到操作项继续往下处理
            filterChain.doFilter(request, response);
        }else{
            HandlerMethod handlerMethod = (HandlerMethod) hanler.getHandler();
            ApiOperation apiOpreation = handlerMethod.getMethod().getAnnotation(ApiOperation.class);
            if(apiOpreation==null){

                filterChain.doFilter(request, response);
            }else {
                ApiInfo apiinfo = apiInfoService.getApiByCode(apiOpreation.code());
                String type = apiOpreation.type();
                //公共服务不用处理
                if ("common".equals(apiinfo.getApiType())) {
                    filterChain.doFilter(request, response);
                } else {
                    Object partnerScope = request.getSession(false).getAttribute(RdisKeyContant.USER_DATASCOPE_SESSION);
                if (partnerScope == null) {

                    PartnerSerivce partnerSerivce = context.getBean(PartnerSerivce.class);
                    UserBean user = CommonUtil.getCrruentUser();
                    Partner partner = partnerSerivce.getPartnerByUser(user.getUserId());
                    if (partner == null) {
                        response.setCharacterEncoding("UTF-8");
                        response.setContentType("application/json; charset=utf-8");
                        PrintWriter pw = null;
                        ResultJson resultJson = ResultJson.failure(ResultCode.NO_AUTH);
                        String res = JSON.toJSONString(resultJson);
                        pw = response.getWriter();
                        pw.append(res);
                    } else {
                        Object json = JSONObject.toJSON(partner);
                        request.getSession(false).setAttribute(RdisKeyContant.USER_DATASCOPE_SESSION, json.toString());
                        filterChain.doFilter(request, response);
                    }
                } else {
                    filterChain.doFilter(request, response);
                }
            }}
    }}
}
